{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": 3
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1597147117434",
   "display_name": "Python 3.6.4 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "url = \"https://so.gushiwen.org/authors/authorvsw_b90660e3e492A1.aspx\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "r = requests.get(url)\n",
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# r.text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "# 匹配模板：首位分别为<div class=\"contson\" xxxxxxxxxxx>和</div>的结构中间的部分\n",
    "# re.S表示可以包括换行\n",
    "pattern = re.compile('<div class=\"contson\" .*?>(.*?)</div>',re.S)\n",
    "poetry = re.findall(pattern, r.text)\n",
    "poetry"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "poetry_list = []\n",
    "for item in poetry:\n",
    "    item = re.sub(\"<br />\",\"\",item)\n",
    "    item = re.sub(\"<p>\",\"\",item)\n",
    "    item = re.sub(\"</p>\",\"\",item)\n",
    "    item = re.sub(\"\\n\",\"\",item)\n",
    "    poetry_list.append(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "poetry_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "\n",
    "for i in range(1,10):\n",
    "    url = \"https://so.gushiwen.org/authors/authorvsw_b90660e3e492A\"+str(i) +\".aspx\"\n",
    "\n",
    "    print(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}